import * as THREE from 'three';

// const geometry = new THREE.BoxGeometry(50, 50, 50);
// const material = new THREE.MeshLambertMaterial()

// const mesh1 = new THREE.Mesh(geometry, material);
// const mesh2 = new THREE.Mesh(geometry, material);
// mesh2.translateX(100);

// // 创建一个组对象
// // const group = new THREE.Group();
// const group = new THREE.Object3D();
// group.add(mesh1);
// group.add(mesh2);

const group = new THREE.Group();
group.name = '高层';
for (let i = 0; i < 5; i++) {
    const geometry = new THREE.BoxGeometry(20, 60, 10);
    const material = new THREE.MeshLambertMaterial({
        color: 0x00ffff
    })
    const mesh = new THREE.Mesh(geometry, material);
    mesh.name = 1 + i + '号楼';
    mesh.position.x = i * 30;
    group.add(mesh);
}

group.position.y = 30;

const group2 = new THREE.Group();
group2.name = '洋房';
for (let i = 0; i < 5; i++) {
    const geometry = new THREE.BoxGeometry(20, 30, 10);
    const material = new THREE.MeshLambertMaterial({
        color: 0x00ffff
    })
    const mesh = new THREE.Mesh(geometry, material);
    mesh.name = 6 + i + '号';
    mesh.position.x = i * 30;
    group2.add(mesh);
}

group2.position.y = 15;
group2.position.z = 50;

const module = new THREE.Group();
module.name = '小区';
module.add(group, group2);

module.position.set(-50, 0, -25)

// 递归遍历所有的模型节点
module.traverse(function(obj){
    // 判断是否房子（只打印房子名称）
    if(obj.isMesh){
        console.log(obj.name);
        // 改变房子的颜色
        obj.material.color.set(0xffff00)
    }
})
// 根据名字找到对象，并进行操作
const obj = module.getObjectByName('4号楼');
obj.material.color.set(0xff0000);

export default module;